#include "configdialog.h"

#include <QListWidget>
#include <QListWidgetItem>
#include <QStackedWidget>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSlider>
#include <QLabel>

namespace Ui {

ConfigDialog::ConfigDialog(QString *host, quint16 *port, quint16 *inactivityTime, QWidget *parent):QDialog(parent), hostValue(host), portValue(port), inactivityTimeValue(inactivityTime){
	setModal(true);

	contentsWidget = new QListWidget;
	contentsWidget->setViewMode(QListView::IconMode);
	contentsWidget->setIconSize(QSize(64, 64));
	contentsWidget->setMovement(QListView::Static);
	contentsWidget->setMaximumWidth(120);
	contentsWidget->setSpacing(12);

	pagesWidget = new QStackedWidget;
	createConnectionConfig();
	createGeneralConfig();

	closeButton = new QPushButton(tr("Close"));
	connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));

	createIcons();
	contentsWidget->setCurrentRow(0);


	horizontalLayout = new QHBoxLayout;
	horizontalLayout->addWidget(contentsWidget);
	horizontalLayout->addWidget(pagesWidget, 1);

	buttonsLayout = new QHBoxLayout;
	buttonsLayout->addStretch(1);
	buttonsLayout->addWidget(closeButton);

	mainLayout = new QVBoxLayout;
	mainLayout->addLayout(horizontalLayout);
	mainLayout->addStretch(1);
	mainLayout->addSpacing(12);
	mainLayout->addLayout(buttonsLayout);
	setLayout(mainLayout);

	setWindowTitle(tr("Configuration Dialog"));
}

ConfigDialog::~ConfigDialog() {
	delete host;
	delete port;
	delete connectionLayout;
	delete connectionConfigWidget;
	delete timeSlider;
	delete generalLayout;
	delete generalConfigWidget;
	delete pagesWidget;
	delete closeButton;
	delete horizontalLayout;
	delete buttonsLayout;
	delete mainLayout;
	delete contentsWidget;
}

void ConfigDialog::createIcons()
{
	QListWidgetItem *configButton = new QListWidgetItem(contentsWidget);
	configButton->setIcon(QIcon("data/img/connect.png"));
	configButton->setText(tr("Connection"));
	configButton->setTextAlignment(Qt::AlignHCenter);
	configButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);

	QListWidgetItem *updateButton = new QListWidgetItem(contentsWidget);
	updateButton->setIcon(QIcon("data/img/general.png"));
	updateButton->setText(tr("General"));
	updateButton->setTextAlignment(Qt::AlignHCenter);
	updateButton->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);


	connect(contentsWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(changePage(QListWidgetItem *, QListWidgetItem*)));
}

void ConfigDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous) {
	if (!current)
	current = previous;
	
	pagesWidget->setCurrentIndex(contentsWidget->row(current));
}

void ConfigDialog::createConnectionConfig() {
	host = new QLineEdit();
	host->setText(*hostValue);
	port = new QLineEdit();
	port->setText(QString::number(*portValue));
	port->setValidator(new QIntValidator(0, 65535, port));
	
	connectionLayout = new QFormLayout();
	connectionLayout->addRow("Host", this->host);
	connectionLayout->addRow("Port", this->port);

	connectionConfigWidget = new QWidget();
	connectionConfigWidget->setLayout(connectionLayout);
	
	connect(host,SIGNAL(textChanged(const QString &)),this,SLOT(changeHost(const QString &)));
	connect(port,SIGNAL(textChanged(const QString &)),this,SLOT(changePort(const QString &)));
	pagesWidget->addWidget(connectionConfigWidget);
}

void ConfigDialog::createGeneralConfig() {
	timeSlider = new QSlider(Qt::Horizontal);
	timeSlider->setRange(0,60);
	timeSlider->setSliderPosition(*inactivityTimeValue);

	timeLabel = new QLabel(QString::number(*inactivityTimeValue));

	timeLayout = new QHBoxLayout();
	timeLayout->addWidget(timeSlider);
	timeLayout->addWidget(timeLabel);
	connect(timeSlider,SIGNAL(valueChanged(int)),timeLabel,SLOT(setNum(int)));
	connect(timeSlider,SIGNAL(valueChanged(int)),this,SLOT(changeInactivityTime(int)));

	generalLayout = new QFormLayout();

	generalLayout->addRow("Inactivity", timeLayout);

	generalConfigWidget = new QWidget();
	generalConfigWidget->setLayout(generalLayout);
	
	pagesWidget->addWidget(generalConfigWidget);		
}

void ConfigDialog::changeHost(const QString &text){
	*hostValue=text;
}
void ConfigDialog::changePort(const QString &text){
	*portValue=text.toInt();
}
void ConfigDialog::changeInactivityTime(int value){
	*inactivityTimeValue=1000*60*value;
}

}

